package com.course.component.http;
/*
@author By 林中小鸟
@package  com.self.introduction.component.http
@create 2021 - 11 - 2021\11\22 0022
@QQ: 357182695
@Em: themooncss@gmail.com
*/

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.component.enums.ResultStatus;
import com.course.component.util.CopyUtil;
import com.course.component.util.TreeUtil;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

/**
 * API接口的基础返回类
 */

@ToString
@Getter
public class Crp<T> {
    /**
     * 是否成功
     */
    private Integer code;

    /**
     * 说明
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public Crp() {

    }

    public Crp(ResultStatus resultStatus, T data) {
        this.code = resultStatus.getCode();
        this.message = resultStatus.getMessage();
        this.data = data;
    }

    public Crp(ResultStatus resultStatus, T data, Class course) {
        this.code = resultStatus.getCode();
        this.message = resultStatus.getMessage();
        this.data = (T) CopyUtil.copy(data, course);;
    }

    public Crp(ResultStatus resultStatus, List data, Class course) {
        this.code = resultStatus.getCode();
        this.message = resultStatus.getMessage();
        this.data = (T) CopyUtil.copyList(data, course);
    }

    public Crp(ResultStatus resultStatus, List data, Class course, Boolean estree) {
        this.code = resultStatus.getCode();
        this.message = resultStatus.getMessage();
        if(estree.equals(true)){
            this.data = (T)TreeUtil.build(CopyUtil.copyList(data, course)) ;
        }else{
            this.data = (T) CopyUtil.copyList(data, course);
        }

    }

    public Crp(ResultStatus resultStatus, Page<T> data, Class course) {
        this.code = resultStatus.getCode();
        this.message = resultStatus.getMessage();
        data.setRecords(CopyUtil.copyList(data.getRecords(), course));
        this.data = (T) data;
    }

    public Crp(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /** 业务成功返回业务代码和描述信息 */
    public static Crp<Void> success() {
        return new Crp<Void>(ResultStatus.SUCCESS, null);
    }

    /** 业务成功返回业务代码,描述和返回的参数 */
    public static <T> Crp<T> success(T data) {
        return new Crp<T>(ResultStatus.SUCCESS, data);
    }

    /** 业务成功返回业务代码,描述和返回的参数 */
    public static <T> Crp<T> success(ResultStatus resultStatus, T data) {
        if (resultStatus == null) {
            return success(data);
        }
        return new Crp<T>(resultStatus, data);
    }

    /** 业务异常返回业务代码和描述信息 */
    public static <T> Crp<T> failure() {
        return new Crp<T>(ResultStatus.INTERNAL_SERVER_ERROR, null);
    }

    /** 业务异常返回业务代码,描述和返回的参数 */
    public static <T> Crp<T> failure(ResultStatus resultStatus) {
        return failure(resultStatus, null);
    }

    /** 业务异常返回业务代码,描述和返回的参数 */
    public static <T> Crp<T> failure(ResultStatus resultStatus, T data) {
        if (resultStatus == null) {
            return new Crp<T>(ResultStatus.INTERNAL_SERVER_ERROR, null);
        }
        return new Crp<T>(resultStatus, data);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
